package controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import pojo.Book;
import pojo.Topic;
import pojo.User;

import java.util.*;

import dao.BookDAO;
import dao.CommentDAO;
import dao.TopicDAO;
import dao.UserDAO;
 
@Controller
public class TimKiemThuongController {
	private static int btt=0;
	private static String input="";
	private static int page=0;
	private static int kieuSort=0;
	private static int topicId=0;
    @RequestMapping(value= "/TimKiemThuong.do" , method = RequestMethod.POST)
    public String getview(HttpServletRequest request,
    		@RequestParam(value="headerinput", required = false) String _input,
    		@RequestParam(value ="trang", required = false) String trang,
			@RequestParam(value ="slbooktt", required = false) String _btt,
			@RequestParam(value ="kieuSort", required = false) String _kieusort,
			@RequestParam(value ="save", required = false) String save,
			@RequestParam(value ="headertopic", required = false) String topic,
			Model model			
    		) {
    	
    	
    	
    	if(topic!=null) topicId=Integer.parseInt(topic);
    	if(save.equals("false")) input="";
    	if(_btt!=null)    	btt=Integer.parseInt(_btt);
    	if(trang!=null) page=Integer.parseInt(trang);
    	if(_kieusort!=null) kieuSort=Integer.parseInt(_kieusort);
    	if(_input!=null&&save.equals("false")) input=_input;
    	HttpSession session = request.getSession();
    	String st2= request.getRequestURL().toString();
    	String oldUrl=st2+"?headerinput="+input+"&trang="+Integer.toString(page)+"&slbooktt="+Integer.toString(btt)+"&kieuSort="+Integer.toString(kieuSort)+"&save=true"+"&headertopic="+Integer.toString(topicId);
    	session.setAttribute("oldUrl",oldUrl);

    	BookDAO bd=new BookDAO();
    	Topic t=null;
    	TopicDAO td= new TopicDAO();
    	if(topicId==-1)
    	{
    		t=null;
    	}
    	else
    	{
    		t=td.getTopicByID(topicId);
    	}
    	long tongsotrang= bd.tinhSoTrangTimKiemNhanh(t,input, btt);
    	List<Book> lb= bd.getBookTimKiemNhanh(t,input, page, btt, kieuSort);
    	long tongketqua =bd.tinhKetquaTimKiemNhanh(t,input, btt);
    	
    	String kq= "Có "+Long.toString(tongketqua) +" kết quả";
    	model.addAttribute("ketqua", kq);
    	model.addAttribute("lb",lb);
    	model.addAttribute("Servlet","TimKiemThuong.do?save=true");
    	model.addAttribute("tongSoTrang",tongsotrang);
    	model.addAttribute("trangHienTai",page);
    	model.addAttribute("slbooktt",btt);
    	model.addAttribute("kieuSort",kieuSort);
    	model.addAttribute("title1","Kết quả tìm kiếm nhanh");
    	model.addAttribute("title2",null);
    	model.addAttribute("tukhoa",input);
    	
    	return ("page/FrontEnd/KetQuaTimKiem");
    }
    @RequestMapping(value= "/TimKiemThuong.do" , method = RequestMethod.GET)
    public String getview2(HttpServletRequest request,
    		@RequestParam(value="headerinput", required = false) String _input,
    		@RequestParam(value ="trang", required = false) String trang,
			@RequestParam(value ="slbooktt", required = false) String _btt,
			@RequestParam(value ="kieuSort", required = false) String _kieusort,
			@RequestParam(value ="save", required = false) String save,
			@RequestParam(value ="headertopic", required = false) String topic,
			Model model
    		) {
    	
    	
    	if(topic!=null) topicId=Integer.parseInt(topic);
    	if(save.equals("false")) input="";
    	if(_btt!=null)    	btt=Integer.parseInt(_btt);
    	if(trang!=null) page=Integer.parseInt(trang);
    	if(_kieusort!=null) kieuSort=Integer.parseInt(_kieusort);
    	if(_input!=null&&save.equals("false")) input=_input;
    	HttpSession session = request.getSession();
    	String st2= request.getRequestURL().toString();
    	String oldUrl=st2+"?headerinput="+input+"&trang="+Integer.toString(page)+"&slbooktt="+Integer.toString(btt)+"&kieuSort="+Integer.toString(kieuSort)+"&save=true"+"&headertopic="+Integer.toString(topicId);
    	session.setAttribute("oldUrl",oldUrl);

    	BookDAO bd=new BookDAO();
    	Topic t=null;
    	TopicDAO td= new TopicDAO();
    	if(topicId==-1)
    	{
    		t=null;
    	}
    	else
    	{
    		t=td.getTopicByID(topicId);
    	}
    	long tongsotrang= bd.tinhSoTrangTimKiemNhanh(t,input, btt);
    	long tongketqua =bd.tinhKetquaTimKiemNhanh(t,input, btt);
    	
    	String kq= "Có "+Long.toString(tongketqua) +" kết quả";
    	model.addAttribute("ketqua", kq);
    	List<Book> lb= bd.getBookTimKiemNhanh(t,input, page, btt, kieuSort);
    	model.addAttribute("lb",lb);
    	model.addAttribute("Servlet","TimKiemThuong.do?save=true");
    	model.addAttribute("tongSoTrang",tongsotrang);
    	model.addAttribute("trangHienTai",page);
    	model.addAttribute("slbooktt",btt);
    	model.addAttribute("kieuSort",kieuSort);
    	model.addAttribute("title1","Kết quả tìm kiếm nhanh");
    	model.addAttribute("title2",null);
    	model.addAttribute("tukhoa",input);
    	return ("page/FrontEnd/KetQuaTimKiem");
    }
    
   
}